<template>
    <div class="shopcar-container">
        <!-- 购物车商品 -->
        <div class="mui-card" v-for="(item,i) in goodslist" :key="item.id">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <!-- 开关 -->
                    <mt-switch v-model="$store.getters.getCheckedStatus[item.id]" @change="getSelectedStatus(item.id,$store.getters.getCheckedStatus[item.id])"></mt-switch>
                    <!-- 商品图片 -->
                    <img :src="item.thumb_path" alt="图片加载失败">
                    <div class="info">
                        <h3>{{ item.title }}</h3>
                        <div class="info-body">
                            <strong>&yen;{{ item.sell_price }}</strong>
                            <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                            <span @click="remove(item.id,i)">删除</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 购物车结算 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner jiesuan-box">
                    <div class="jiesuan">
                        <p>总计(不含运费)</p>
                        <div>
                            已勾选商品 <strong>{{ $store.getters.getGoodsCountAndAmount.count }}</strong> 件, 总价: <strong>&yen; {{ $store.getters.getGoodsCountAndAmount.amount }}</strong>
                        </div>
                    </div>
                    <mt-button type="danger" size="small">去结算</mt-button>
                </div>
            </div>
        </div>

        <p>{{ $store.getters.getGoodsSelected }}</p>
    </div>
</template>

<script>
import { Toast } from 'mint-ui';
import numbox from '../subcomponents/ShopCar_numbox.vue';
export default {
    data () {
        return {
            goodslist: []
        }
    },
    created () {
        this.getGoodsList()
    },
    methods: {
        // 获取商品列表
        getGoodsList(){
            var idArr = [];

            this.$store.state.car.forEach(item=>idArr.push(item.id));

            // 判断购物车里是否有数据
            if (idArr.length <= 0) {
                return;
            }

            this.$http.get('api/goods/getshopcarlist/' + idArr.join(',')).then(res=>{
                // console.log(res);
                if (res.body.status === 0) {
                    this.goodslist = res.body.message;
                } else {
                    Toast('获取商品数据失败')
                }
            })
        },

        // 删除页面上的商品
        remove(id,index){
            this.goodslist.splice(index,1);
            this.$store.commit('removeStoreDate',id);
        },

        // 商品选中状态开关
        getSelectedStatus(id,val){
            // 每当点击开关，把最新的 开关状态，同步到 store 中
            console.log(val);
            this.$store.commit('updateGoodsSelected',{id,selected:val});
        }

    },
    components: {
        numbox
    }
}
</script>

<style lang="less" scoped>
    .shopcar-container {
        background-color: #eee;
        overflow: hidden;
        .mui-card {
            .mui-card-content-inner {
                display: flex;
                // justify-content: space-between;
                align-items: center;
                img {
                    width: 80px;
                    height: auto;
                }
                .info {
                    padding-left: 10px;
                    h3 {
                        font-size: 13px;
                        margin-bottom: 15px;
                    }
                    .info-body {
                        strong {
                            color: #f04;
                            padding-right: 8px;
                        }
                        .mui-numbox {
                            margin:5px 10px 5px 0;
                            height: 30px;
                        }
                        span {
                            
                            color: #26a2ff;
                        }

                    }
                }
            }
        }

        .jiesuan-box {
            display: flex;
            justify-content: space-between;
            .jiesuan {
                font-size: 14px;
                color: #666;
                strong {
                    font-size: 16px;
                    color: #f04;
                }
            }
            .mint-button--small {
                font-size: 16px;
                padding: 0 15px;
                height: 38px;
            }
        }
    }
</style>


